package com.squirrel.common.service.impl;

import com.squirrel.common.entity.Channel;
import com.squirrel.common.exception.BaseException;
import com.squirrel.common.mapper.ChannelMapper;
import com.squirrel.common.response.code.CodeEnum;
import com.squirrel.common.service.ChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.squirrel.common.vo.channel.ChannelParentVO;
import com.squirrel.common.vo.channel.ChannelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * ChannelServiceImp 频道服务实现类
 * </p>
 *
 * @author 关远键
 * @since 2021/11/11 16:06
 */
@Service
public class ChannelServiceImp extends ServiceImpl<ChannelMapper, Channel> implements ChannelService {


    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 查询所有的根频道
     * @author 关远键
     * @date 2021/11/13 21:39
     * @return java.util.List<com.squirrel.common.vo.channel.ChannelVO>
     */
    @Override
    public List<ChannelVO> getChannelParentList() {
       return getChannelByParent(0);
    }

    /**
     * 根据父频道编号查询其所有子频道
     * @author 关远键
     * @date 2021/11/13 22:15
     * @param parentId
     * @return java.util.List<com.squirrel.common.vo.channel.ChannelVO>
     */
    @Override
    public List<ChannelVO> getChannelByParent(Integer parentId) {

        List<Channel> channelList = channelMapper.getChannelListByParentId(parentId);

        if (null == channelList) {
//            throw new BaseException(CodeEnum.NON_RESOURCE);
            return null;
        }

        ArrayList<ChannelVO> list = new ArrayList<>();
        for (Channel channel: channelList) {
            ChannelVO vo = ChannelVO.builder()
                    .id(channel.getChannelId())
                    .title(channel.getChannelTitle())
                    .build();
            list.add(vo);
        }

        return list;
    }

    /**
     * 返回级联频道
     * @author 关远键
     * @date 2021/11/14 9:28
     * @return java.util.List<com.squirrel.common.vo.channel.ChannelParentVO>
     */
    @Override
    public List<ChannelParentVO> getChannelList() {
        ArrayList<ChannelParentVO> parentList = new ArrayList<>();
        List<ChannelVO> channels = getChannelByParent(0);

        for (ChannelVO channel: channels) {

            ChannelParentVO parentVO = new ChannelParentVO();
            parentVO.setId(channel.getId());
            parentVO.setTitle(channel.getTitle());

            ChannelParentVO child = getChannelWithParent(channel.getId());
            if (null != child) {
                ArrayList<ChannelVO> childList = new ArrayList<>();
                childList.add(child);
                parentVO.setChild(childList);
            }

            parentList.add(parentVO);
        }


        return parentList;
    }

    /**
     * 根据父频道ID查询其子频道及其子子频道
     * 缺点：递归调用，时间复杂度极其高，耗资源
     * @author 关远键
     * @date 2021/11/14 9:29
     * @param parentId
     * @return com.squirrel.common.vo.channel.ChannelParentVO
     */
    private ChannelParentVO getChannelWithParent(Integer parentId) {
        List<ChannelVO> channelList = getChannelByParent(parentId);

        if (null == channelList) {
            return null;
        }

        ChannelParentVO parentVO = new ChannelParentVO();
        Channel channel = channelMapper.getChannelById(parentId);
        parentVO.setId(channel.getChannelId());
        parentVO.setTitle(channel.getChannelTitle());

        ArrayList<ChannelVO> child = new ArrayList<>();
        for (ChannelVO item: channelList) {
            if (null == getChannelByParent(parentId)) {
                child.add(item);
            } else {
                ChannelParentVO parent = getChannelWithParent(item.getId());
                child.add(parent);
            }
        }

        parentVO.setChild(child);

        return parentVO;
    }


}
